import 'dart:async';
import 'package:bili_caricature/pages/book_shelf/repository/home_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';
import '../../../base/base_state.dart';
import '../../../models/response_model.dart';
import '../../../widgets/easy_loading.dart';
import '../model/cartoon_model_entity.dart';
import '../model/cartoon_recommend_entity.dart';
import '../model/cartoon_series_entity.dart';

part 'novel_detail_event.dart';
part 'novel_detail_state.dart';

class NovelDetailBloc extends Bloc<NovelDetailEvent, NovelDetailState> {
  NovelDetailBloc() : super(NovelDetailState()) {
    on<GetNovelDetailEvent>(_getNovelDetailEvent);
  }

  Future<void> _getNovelDetailEvent(event, emit) async {
    XsEasyLoading.showLoading();

    /// 主数据
    ResponseModel? responseModel = await HomeRepository.getMainData<CartoonModelData>();

    /// 同系列数据
    ResponseModel? responseModel2 = await HomeRepository.getSeriesData<CartoonSeriesData>();

    /// 推荐数据
    ResponseModel? responseModel3 = await HomeRepository.getRecommendData<CartoonRecommendData>();

    XsEasyLoading.dismiss();
    CartoonModelData? mainModel = responseModel.data;
    CartoonSeriesData cartoonSeriesData = responseModel2.data;
    List<CartoonSeriesDataSeriesComics>? seriesList = cartoonSeriesData.seriesComics;
    CartoonRecommendData cartoonRecommendData = responseModel3.data;
    List<CartoonRecommendDataInfos>? recommendList = cartoonRecommendData.infos;
    NetState netState = NetState.dataSuccessState;
    emit(state.clone()
      ..mainModel = mainModel
      ..seriesList = seriesList
      ..recommendList = recommendList
      ..netState = netState);
  }
}
